package setting

import (
	"github.com/go-ini/ini"
	"log"
	"time"
)

// App setting
type App struct {
	JwtSecret       string
	PageSize        int
	RuntimeRootPath string

	PrefixUrl      string
	ImageSavePath  string
	ImageMaxSize   int
	ImageAllowExts []string

	ExportSavePath string
	QrCodeSavePath string
	FontSavePath   string

	LogSavePath string
	LogSaveName string
	LogFileExt  string
	TimeFormat  string
}

var AppSetting = &App{}

//Server setting
type Server struct {
	RunMode      string
	HttpPort     int
	HttpsPort    int
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	Env          string
}

var ServerSetting = &Server{}

type Rtsp struct {
	AutoPull   string
	RemoteAddr string
	ListenAddr string
}

var RtspSetting = &Rtsp{}

var cfg *ini.File

func Setup() {
	var err error
	cfg, err = ini.Load("conf/app.ini")
	if err != nil {
		log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
	}
	AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024
	ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
	ServerSetting.WriteTimeout = ServerSetting.WriteTimeout * time.Second

	mapTo("app", AppSetting)
	mapTo("server", ServerSetting)
	mapTo("rtsp", RtspSetting)
}

// mapTo map section
func mapTo(section string, v interface{}) {
	err := cfg.Section(section).MapTo(v)
	if err != nil {
		log.Fatalf("Cfg.MapTo %s err: %v", section, err)
	}
}
